﻿//---------------------------------------------------------------------------------
// Copyright 2011 Microsoft Corporation
// Licensed under the Microsoft Public License (MS-PL) (the "License"); 
// You may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.opensource.org/licenses/ms-pl.html
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR 
// CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, 
// INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR 
// CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
// MERCHANTABLITY OR NON-INFRINGEMENT. 
// See the Microsoft Public License (MS-PL) for the specific language governing 
// permissions and limitations under the License.
//---------------------------------------------------------------------------------

using System;
using System.Windows;
using System.Windows.Controls;

namespace Microsoft.Telco.Controls.WindowsLive
{
    /// <summary>
    /// Control that displays user presence information.
    /// </summary>
    #region Parts and States Model Declarations
    [TemplatePart(Name = "StatusIconButton", Type = typeof(Button))]
    [TemplatePart(Name = "UserNameOrEmailTextBlock", Type = typeof(TextBlock))]
    #endregion
    public class WLPresenceControl : Control
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="WLContactsControl"/> class.
        /// </summary>
        public WLPresenceControl()
        {
            this.DefaultStyleKey = typeof(WLPresenceControl);
        }

        /// <summary>
        /// When overridden in a derived class, is invoked whenever application code or internal processes (such as a rebuilding layout pass) call <see cref="M:System.Windows.Controls.Control.ApplyTemplate"/>.
        /// </summary>
        public override void OnApplyTemplate()
        {
            StatusIconButton = GetTemplateChild("StatusIconButton") as Button;
            if (StatusIconButton != null)
            {
                SetStatus(this.UserStatus, StatusIconButton);
            }

            UserNameOrEmailTextBlock = GetTemplateChild("UserNameOrEmailTextBlock") as TextBlock;
            if (UserNameOrEmailTextBlock != null)
            {
                UserNameOrEmailTextBlock.Text = UserNameOrEmail;
                UserNameOrEmailTextBlock.MouseLeftButtonDown += (s, e) =>
                {
                    if (UserSelected != null)
                    {
                        UserSelected(s, e);
                    }
                };
            }
        }

        #region parts
        /// <summary>
        /// Gets or sets the status icon.
        /// </summary>
        /// <value>The status icon.</value>
        public Button StatusIconButton { get; set; }

        /// <summary>
        /// Gets or sets the user name or email text block.
        /// </summary>
        /// <value>The user name or email text block.</value>
        public TextBlock UserNameOrEmailTextBlock { get; set; }
        #endregion

        #region dependency properties
        /// <summary>
        /// Gets or sets the StatusIconHeight for WLPresenceControl.
        /// </summary>
        /// <value>The StatusIconHeight.</value>
        public double StatusIconHeight
        {
            get { return (double)GetValue(StatusIconHeightProperty); }
            set { SetValue(StatusIconHeightProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for StatusIconHeight for WLPresenceControl.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty StatusIconHeightProperty =
            DependencyProperty.Register("StatusIconHeight", typeof(double), typeof(WLPresenceControl),
            new PropertyMetadata(10.0D,
                        new PropertyChangedCallback((obj, args) =>
                        {
                            WLPresenceControl control = (WLPresenceControl)obj;
                            if (control.StatusIconButton != null)
                            {
                                control.StatusIconButton.Height = (double)args.NewValue;
                            }
                        }))); 
        
        /// <summary>
        /// Gets or sets the StatusIconWidth for WLPresenceControl.
        /// </summary>
        /// <value>The StatusIconWidth.</value>
        public double StatusIconWidth
        {
            get { return (double)GetValue(StatusIconWidthProperty); }
            set { SetValue(StatusIconWidthProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for StatusIconWidth for WLPresenceControl.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty StatusIconWidthProperty = 
            DependencyProperty.Register("StatusIconWidth", typeof(double), typeof(WLPresenceControl), 
            new PropertyMetadata(10.0D, 
                        new PropertyChangedCallback((obj,args)=>{
                            WLPresenceControl control = (WLPresenceControl)obj;
                            if (control.StatusIconButton != null)
                            {
                                control.StatusIconButton.Width = (double)args.NewValue;
                            }
                })));
        
        /// <summary>
        /// Gets or sets the UserNameOrEmail for WLPresenceControl.
        /// </summary>
        /// <value>The UserNameOrEmail.</value>
        public string UserNameOrEmail
        {
            get { return (string)GetValue(UserNameOrEmailProperty); }
            set { SetValue(UserNameOrEmailProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for UserNameOrEmail for WLPresenceControl.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty UserNameOrEmailProperty = 
            DependencyProperty.Register("UserNameOrEmail", typeof(string), typeof(WLPresenceControl), 
            new PropertyMetadata(string.Empty, 
                        new PropertyChangedCallback((obj,args)=>{
                            WLPresenceControl control = (WLPresenceControl)obj;
                            if (control.UserNameOrEmailTextBlock != null)
                            {
                                control.UserNameOrEmailTextBlock.Text = args.NewValue as string;
                            }
                })));
        
        /// <summary>
        /// Gets or sets the UserStatus for WLPresenceControl.
        /// </summary>
        /// <value>The UserStatus.</value>
        public Status UserStatus
        {
            get { return (Status)GetValue(UserStatusProperty); }
            set { SetValue(UserStatusProperty, value); }
        }

        /// <summary>
        /// This DependencyProperty acts as the backing store for UserStatus for WLPresenceControl.  This enables animation, styling, binding, etc.
        /// </summary>
        public static readonly DependencyProperty UserStatusProperty = 
            DependencyProperty.Register("UserStatus", typeof(Status), typeof(WLPresenceControl), 
            new PropertyMetadata(Status.Unknown, 
                        new PropertyChangedCallback((obj,args)=>{
                            WLPresenceControl control = (WLPresenceControl)obj;
                            if (control.StatusIconButton != null)
                            {
                                SetStatus((Status)args.NewValue, control.StatusIconButton);
                                if (control.UserNameOrEmailTextBlock.Text != string.Empty)
                                {
                                    control.StatusIconButton.Visibility = Visibility.Visible;
                                }
                            }
                })));
        #endregion

        #region events
        /// <summary>
        /// Occurs when [user is selected]. UserName/Email is passed as event data.
        /// </summary>
        public event EventHandler UserSelected;
        #endregion

        #region private methods
        private static void SetStatus(Status status, Button statusIcon)
        {
            Style statusStyle = null;
            switch (status)
            {
                case Status.Available:
                    statusStyle = ResourceHelper.CommonStyles[Constants.AvailableIconKey] as Style;
                    break;
                case Status.Busy:
                    statusStyle = ResourceHelper.CommonStyles[Constants.BusyIconKey] as Style;
                    break;
                case Status.Away:
                    statusStyle = ResourceHelper.CommonStyles[Constants.AwayIconKey] as Style;
                    break;
                case Status.Offline:
                    statusStyle = ResourceHelper.CommonStyles[Constants.OfflineIconKey] as Style;
                    break;
                case Status.Unknown:
                    statusStyle = ResourceHelper.CommonStyles[Constants.OfflineIconKey] as Style;
                    break;
            }
            statusIcon.Style = statusStyle;
        }
        #endregion
    }
}
